package com.caigou.controller;

import com.caigou.annotation.OperLog;
import com.caigou.bean.cms.entity.ArticlePromoter;
import com.caigou.bean.cms.param.ArticlePromoterQueryParam;
import com.caigou.bean.cms.param.ArticlePromoterParam;
import com.caigou.bean.exam.entity.Users;
import com.caigou.constant.NewsConstant;
import com.caigou.enums.ResponseEnum;
import com.caigou.security.utils.CurrentUser;
import com.caigou.service.ArticlePromoterService;
import com.caigou.service.PageService;
import com.caigou.util.Result;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

//稿件作者管理
@Slf4j
@RestController
@RequestMapping("/articlePromoter/")
public class ArticlePromoterController {
    @Resource
    private ArticlePromoterService articlePromoterService;
    @Resource
    private PageService pageService;

    /**
     * 新增作者
     * @param file
     * @param name
     * @param authorPost
     * @param authorIntro
     * @param contractInfo
     * @param notes
     * @param user
     * @return
     */
    @OperLog(operModule = NewsConstant.ARTICLE_PROMOTER_MODULE, operType = NewsConstant.ADD_OPERATION, operDesc = "增加作者")
    @RequestMapping("saveOrUpdateArticlePromoter")
    @PreAuthorize("hasAnyAuthority('zuozheguanli')")
    public Result saveOrUpdaterticlePromoter(@RequestParam(value="file", required = false) MultipartFile file, @RequestParam(value="id", required = false)Integer id, @RequestParam(value="name", required=true)String name,
                                @RequestParam(value="authorPost", required=false)String authorPost, @RequestParam(value="authorIntro", required=false)String authorIntro,
                                @RequestParam(value="contractInfo", required = false)String contractInfo, @RequestParam(value="notes", required = false)String notes,
                                @CurrentUser Users user){
        log.info("---------------file={}-------------", file);
        if(name!=null && name.length()>5){
            return Result.error("作者姓名不能超过5个字符", "作者姓名不能超过5个字符");
        }
        if(authorPost!=null && authorPost.length()>6){
            return Result.error("作者职务不能超过6个字符", "作者职务不能超过6个字符");
        }
        if(authorIntro!=null && authorIntro.length()>80){
            return Result.error("作者简介不能超过80个字符", "作者简介不能超过80个字符");
        }
        if(notes!=null && notes.length()>50){
            return Result.error("备注不能超过50个字符", "备注不能超过50个字符");
        }
        return articlePromoterService.saveOrUpdateArticlePromoter(file, id, name, authorPost, authorIntro, contractInfo, notes, user.getUserid());
    }

    /**
     * 查询作者列表
     * @param articlePromoterQueryParam
     * @return
     */
    @RequestMapping("queryArticlePromoter")
    @PreAuthorize("hasAnyAuthority('chakanzuozhe')")
    public Result queryArticlePromoter(@RequestBody ArticlePromoterQueryParam articlePromoterQueryParam){
        if(articlePromoterQueryParam.getPageNum()!=null || articlePromoterQueryParam.getPageSize()!=null) {
            PageInfo<ArticlePromoter> articlePromoterPageInfo = articlePromoterService.queryArticlePromoter(articlePromoterQueryParam);
            return Result.success(articlePromoterPageInfo);
        }else{
            List<ArticlePromoter> articlePromoterList = articlePromoterService.queryArticlePromoterAll(articlePromoterQueryParam);
            return Result.success(articlePromoterList);
        }
    }

    /**
     * 获取作者详情
     * @param articlePromoterParam
     * @return
     */
    @RequestMapping("getArticlePromoter")
    @PreAuthorize("hasAnyAuthority('chakanzuozhe')")
    public Result getArticlePromoter(@RequestBody ArticlePromoterParam articlePromoterParam){
        return articlePromoterService.getArticlePromoter(articlePromoterParam);
    }

    /**
     * 更新作者禁用状态---》更新到正常或者禁用都支持
     * @param articlePromoterParam
     * @return
     */
    @OperLog(operModule = NewsConstant.ARTICLE_PROMOTER_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "修改作者禁用、启用状态")
    @RequestMapping("updateArticlePromoterIsForbid")
    @PreAuthorize("hasAnyAuthority('zuozheguanli')")
    public Result updateArticlePromoterIsForbid(@RequestBody ArticlePromoterParam articlePromoterParam){
        return articlePromoterService.updateArticlePromoterIsForbid(articlePromoterParam);
    }

    /**
     * 发布/更新推荐作者的更多文章列表html页面
     * @return
     */
    @RequestMapping("publishAuthorPostListHtml")
    @PreAuthorize("hasAnyAuthority('zuozheguanli')")
    public Result publishAuthorPostListHtml(@CurrentUser Users user){
        String url = pageService.buildAuthorPostListHtml();
        return Result.success(url);
    }
}
